package com.kitty.game.activity.service.vip;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.activity.service.other.GodsDescendToEarthHandler;
import com.kitty.game.config.NPC;
import com.kitty.game.drop.newservice.NewDropService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.npc.message.RespNpcContent;
import com.kitty.game.player.model.PlayerExtendBox;
import com.kitty.game.role.model.Role;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class SuperVipService {
    //获取道行倍数
    public Double getSuperVipRate(Role role){
        PlayerExtendBox extendBox = role.getExtendBox();
        int vipLv = extendBox.getSuperVipType();
        if(DataCache.REDIS_DATA.containsKey("super_vip_" + vipLv)){
            String vipSet = DataCache.REDIS_DATA.get("super_vip_" + vipLv);
            JSONObject obj  =JSONObject.parseObject(vipSet);
            return obj.getDouble("beishu");
        }
        return 1.0;
    }
    //会员等级是否升级
    public void changeSuperVip(Role role){
        PlayerExtendBox extendBox = role.getExtendBox();
        int oldVipLv = extendBox.getSuperVipType();
        int newViplv = 0;
        //累计充值
        int sum = role.getTotalCharge();
        for (int i = 1; i <= 10; i++) {
            if(DataCache.REDIS_DATA.containsKey("super_vip_" + i)){
                String vipSet =   DataCache.REDIS_DATA.get("super_vip_" + i);  //"{\"item\":\"积分-500\",\"task\":[{\"SCHOOL_TASK\":5}],\"money\":100}";
                JSONObject obj  =JSONObject.parseObject(vipSet);
                Integer integer = obj.getInteger("money");
                //如果
                if(sum>=integer){
                    newViplv = i;
                }
            }
        }
        if(newViplv > oldVipLv){
            //升级了
            extendBox.setSuperVipType(newViplv);
            role.save();
        }else{
            //没有升级
        }

    }

    public void chakanVipTypeContent(Role role,NPC npc){
        RespNpcContent respNpcContent = new RespNpcContent();
        respNpcContent.setNpcName(npc.getName());
        respNpcContent.setNpcId((int) npc.getId());
        PlayerExtendBox extendBox = role.getExtendBox();
        int superVipType = extendBox.getSuperVipType();
        respNpcContent.setContent("当前VIP等级：#R" + superVipType + "[离开]");
        respNpcContent.setNpcIcon(npc.getIcon());
        MessagePusher.pushMessage(role, respNpcContent);
    }
    //每日领取奖励
    public void getSuperVipReward(Role role){
        PlayerExtendBox extendBox = role.getExtendBox();
        int superVipType = extendBox.getSuperVipType();
        if(DataCache.REDIS_DATA.containsKey("super_vip_" + superVipType)){
            int remainCount = SpringUtils.getActivityService().getRemainCount(role, ActivityType.vip_reward);
            if (remainCount <= 0) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("今日已经领取奖励了！"));
                return;
            }
            String reward = DataCache.REDIS_DATA.get("super_vip_" + superVipType);
            JSONObject obj  =JSONObject.parseObject(reward);
            JSONArray taskArray = obj.getJSONArray("task");
            String item = obj.getString("item");
            //发送奖励
            for (String s : item.split("\\+")) {
                SpringUtils.getBean(NewDropService.class).addRewardTask(role,s,"");
            }
            //减少任务次数
            for (Object o : taskArray) {
                JSONObject taskObj = (JSONObject)o;
                for (Map.Entry<String, Object> stringObjectEntry : taskObj.entrySet()) {
                    ActivityType activityType = ActivityType.getActivityType(stringObjectEntry.getKey());
                    if(activityType != null){
                        SpringUtils.getActivityService().reduceFinishCount(role, activityType, Integer.parseInt(taskObj.getString(stringObjectEntry.getKey())));

                    }
                }
            }
            SpringUtils.getActivityService().addFinishCount(role, ActivityType.vip_reward, 1);
        }else{
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("您还不是VIP会员！"));
        }

    }

    public static void main(String[] args) {
        String reward = "{\"item\":\"积分-500\",\"task\":[{\"SCHOOL_TASK\":5}]}";//DataCache.REDIS_DATA.get("super_vip_" + superVipType);
        JSONObject obj  =JSONObject.parseObject(reward);
        JSONArray taskArray = obj.getJSONArray("task");
        String item = obj.getString("item");
        //发送奖励
        for (String s : item.split("\\+")) {
            //SpringUtils.getBean(NewDropService.class).addRewardTask(role,s,"");
        }
        //减少任务次数
        for (Object o : taskArray) {
            JSONObject taskObj = (JSONObject)o;
            for (Map.Entry<String, Object> stringObjectEntry : taskObj.entrySet()) {
                ActivityType activityType = ActivityType.getActivityType(stringObjectEntry.getKey());
                if(activityType != null){
                    //SpringUtils.getActivityService().reduceFinishCount(role, activityType, Integer.parseInt(taskObj.getString(stringObjectEntry.getKey())));
                    System.out.println(taskObj.getString(stringObjectEntry.getKey()));
                }
            }
        }
    }

}
